
# vim: fileencoding=utf-8
#!-*- coding:utf-8 -*-
#日本語
import cgi
import logging
from django.utils import simplejson
import os
import wsgiref.handlers
import datetime
from google.appengine.api import users
from google.appengine.ext import db,webapp
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
from common import Category,DayReserve,Spending,Daymemo
import common
class ReserveCron(webapp.RequestHandler):
  def get(self):
    d=datetime.date.today()
    if d.day == 1:
      datas = DayReserve.all()
      for data in datas:
        if not data.last_date or data.last_date <  datetime.datetime(d.year,d.month,1):
          reserve_day = datetime.datetime(d.year,d.month,data.day)
          user=data.user
          content=data.content
          price = data.price
          category = data.category
          spending = Spending(user=user,
                            content=content,
                            price = price,
                            category = category,
                            date = reserve_day)
          spending.put()
          data.last_date = reserve_day
          data.put()
          common.setMemCacheData(user,spending.category,d.year,d.month,price)
      self.response.out.write('success')

def main():
  logging.getLogger().setLevel(logging.DEBUG)
  application = webapp.WSGIApplication(
                                       [('/reserve/dayly', ReserveCron),
                                         ],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
  main()
